﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DDU.Library;
using DDU.DataModel;
using System.Linq;

namespace DDU.ViewModel
{
    public class SemesterEditViewModel : MainViewModel
    {
        #region Properties
        private string guid;
        public string Guid
        {
            get { return guid; }
            set
            {
                guid = value;
                RaisePropertyChanged(() => Guid);
                if (Guid != null)
                {
                    CurrentEditSemester = Database.CollectionOfSemesters.FirstOrDefault(s => s.Guid == Guid);

                    RaisePropertyChanged(() => CurrentEditSemester);
                }
            }
        }

        private SemesterInfo currentEditSemester;
        public SemesterInfo CurrentEditSemester
        {
            get
            {
                return currentEditSemester;
            }
            set
            {
                currentEditSemester = value;
                RaisePropertyChanged(() => CurrentEditSemester);
            }
        }
        #endregion

        #region Commands
        public ICommand UpdateCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        Update(CurrentEditSemester);
                        MessageBox.Show("更新成功!");
                        Navigate(BackUri);
                    }
                };
            }
        }

        public ICommand CancelCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        if (BackUri != null)
                        {
                            Navigate(BackUri);
                        }
                    }
                };
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate
                    {
                        if (MessageBox.Show("确定要删除这条信息吗？", "删除确认", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                        {
                            Remove(CurrentEditSemester);
                            Navigate(BackUri);
                        }
                    }
                };
            }
        }
        #endregion

        #region Method
        private void Remove(SemesterInfo entity)
        {
            Database.CollectionOfSemesters.DeleteOnSubmit(entity);
            Submit();
        }

        private void Update(SemesterInfo entity)
        {
            SemesterInfo item = Database.CollectionOfSemesters.FirstOrDefault(s => s.Guid == entity.Guid);
            item = entity;
            Submit();
        }
        #endregion
    }
}
